package tt.dz.scheduletask.tasks;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tt.dz.dao.ext.charger.OmsChargerBespeakBillRecordDao;
import tt.dz.scheduletask.ScheduledTask;
import tt.dz.service.middleware.OmsBespeakService;
import tt.dz.util.DateUtils;
import tt.dz.util.StringUtil;
import tt.dz.vo.os.RetVO.OrdersStatus;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class UpdateOrdersStatusTask implements ScheduledTask
{
	private Log log = LogFactory.getLog(UpdateOrdersStatusTask.class);

	@Autowired
	OmsChargerBespeakBillRecordDao omsChargerBespeakBillRecordDao;

	@Autowired
	OmsBespeakService omsBespeakService;

	@Override
	public void execute()
	{
		//凌晨十分钟不执行
        if (DateUtils.isInDate(new Date(), "00:00:00", "00:10:00")) {
        	log.info("UpdateOrdersStatusTask 退出执行：凌晨十分钟不执行");
        	return;	
		}
		try
		{
			StringBuffer sql = new StringBuffer();
			sql.append("select '1' type,group_concat(order_no) orderNos from oms_charger_bespeak_bill_record where valid_flag=1 and is_done=0 and DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=start_time ");
			sql.append("union select '2' type,group_concat(order_no) orderNos from oms_charger_charge_bill_record where valid_flag=1 and status=5 and DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=start_time");

			List<Map<String, Object>> listTmp = omsChargerBespeakBillRecordDao.findDataBySql(sql.toString());
			for(Map<String, Object> mapTmp : listTmp)
			{
				try
				{
					String orderNos= "";
					if ("1".equals((String)mapTmp.get("type"))) {   //预约订单
						orderNos = (String)mapTmp.get("orderNos");					
						if (StringUtil.isNotBlank(orderNos)) {
							List<OrdersStatus> listOrdersStatus = omsBespeakService.getOrdersStatus(orderNos);	
							if (listOrdersStatus.size()>0) {
							    orderNos = "";
								for (OrdersStatus ordersStatus : listOrdersStatus){
									if ("7".equals(ordersStatus.getStatusCode())) {
										orderNos = orderNos + "'" + ordersStatus.getOrderNo() + "',";
									}
								}
								if (StringUtil.isNotBlank(orderNos)) {
									orderNos = orderNos.substring(0, orderNos.length()-1);
									omsBespeakService.updateOrdersStatus(orderNos,"1");
								}
							}
						}
                       
					}
					if ("2".equals((String)mapTmp.get("type"))) {   //预约订单
						orderNos = (String)mapTmp.get("orderNos");					
						if (StringUtil.isNotBlank(orderNos)) {
							List<OrdersStatus> listOrdersStatus = omsBespeakService.getOrdersStatus(orderNos);	
							if (listOrdersStatus.size()>0) {
							    orderNos = "";
								for (OrdersStatus ordersStatus : listOrdersStatus){
									if ("7".equals(ordersStatus.getStatusCode())) {
										orderNos = orderNos + "'" + ordersStatus.getOrderNo() + "',";
									}
								}
								if (StringUtil.isNotBlank(orderNos)) {
									orderNos = orderNos.substring(0, orderNos.length()-1);
									omsBespeakService.updateOrdersStatus(orderNos,"2");
								}
							}
						}
					}
					
//					log.info(map);
				}
				catch (Exception e)
				{
					log.error(e.getMessage(), e);
				}
			}
		}
		catch (Exception e)
		{
			log.error(e.getMessage(), e);
		}
	}
}
